/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import nl.cloudfarming.client.messaging.Message;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.util.Date;
import org.junit.Assert;
import org.junit.Test;
import org.uispec4j.UISpec4J;
import org.uispec4j.Panel;
import org.uispec4j.finder.ComponentMatcher;
import static org.mockito.Mockito.*;

/**
 * Tests for the messageInbox
 * @author Merijn Zengers
 */
public class MessageInboxPanelIT {

    private final InboxRowComponentMatcher inboxRowComponentMatcher = new InboxRowComponentMatcher();
    private final MesagePanelComponentMatcher mesagePanelComponentMatcher = new MesagePanelComponentMatcher();
    
    
    static {
        UISpec4J.init();
    }

    /**
     * Test of addMessage method, of class MessageInboxPanel.
     */
    @Test
    public void testAddMessage() {
        MessageInboxPanel instance = new MessageInboxPanel(); 
        Panel visualPanel = new Panel(instance);
        
        Assert.assertFalse(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());
        Message.Builder messageBuilder = new Message.Builder();
        Date dateReceived = new Date();
        messageBuilder.setOrigin("from").setTitle("title").setDateReceived(dateReceived);
        
        instance.addMessage(messageBuilder.build());

        Assert.assertTrue(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());

    }

    /**
     * Test of myEventOccurred method, of class MessageInboxPanel.
     */
    @Test
    public void testMyEventOccurred() {
        MessageInboxPanel instance = new MessageInboxPanel(); 
        Panel visualPanel = new Panel(instance);
        
        //Test if the inboxRow is not present
        Assert.assertFalse(visualPanel.containsComponent(inboxRowComponentMatcher).isTrue());
        
        Message.Builder messageBuilder = new Message.Builder();
        Date dateReceived = new Date();
        messageBuilder.setOrigin("from").setTitle("title").setDateReceived(dateReceived).setMessageText("messageText");
        
        //Add a message the inboxRow should be created
        instance.addMessage(messageBuilder.build());
        
        //Set selected on the InboxRow
        ((InboxRow)visualPanel.getSwingComponents(inboxRowComponentMatcher)[0]).focusGained(new FocusEvent(instance, 0));
        
        //Check if the messagePanel does not already contain the messageText
        Assert.assertFalse(((MessageDetailPanel)visualPanel.getSwingComponents(mesagePanelComponentMatcher)[0]).getMessageText().equals("messageText")) ;
        
        //Update event for the messageInbox. Lets it know that a selection has changed
        instance.propertyChange(mock(PropertyChangeEvent.class));
        
        Assert.assertTrue(visualPanel.containsComponent(mesagePanelComponentMatcher).isTrue());
        
        //Check if the text apears
        Assert.assertTrue(((MessageDetailPanel)visualPanel.getSwingComponents(mesagePanelComponentMatcher)[0]).getMessageText().equals("messageText")) ;

    }
    
    class InboxRowComponentMatcher implements ComponentMatcher {
        @Override
        public boolean matches(Component component) {
            return (component instanceof InboxRow);
        }
    }
    
    class MesagePanelComponentMatcher implements ComponentMatcher {
        @Override
        public boolean matches(Component component) {
            return (component instanceof MessageDetailPanel);
        }
    }
}
